﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Text.RegularExpressions;

namespace PlanningGIS.MetadataEdit
{
    public partial class FrmDetail : Form
    {
        private DataTable dataTable;
        private DataGridViewRow dataGridViewRow;

        public FrmDetail(DataTable dt,DataGridViewRow dgvr)
        {
            InitializeComponent();

            dataTable = dt;
            dataGridViewRow = dgvr;
        }

        private void FrmDetail_Load(object sender, EventArgs e)
        {
            if (dataTable == null || dataGridViewRow == null) return;

            dataGridView1.Rows.Clear();

            foreach (DataColumn dc in dataTable.Columns)
            {
                if (dc.ColumnName == "ID") continue;
                if (dc.ColumnName == "序号") continue;

                dataGridView1.Rows.Add(dc.ColumnName, dataGridViewRow.Cells[dc.ColumnName].Value);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dgvr in dataGridView1.Rows)
            {
                dataGridViewRow.Cells[dgvr.Cells[0].Value.ToString ()].Value = dgvr.Cells[1].Value;
            }
            this.Close();
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (dataTable == null) return;
            Type t = dataTable.Columns[dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString()].DataType;
            if(dataGridView1.Rows[e.RowIndex].Cells[1].Value == null) return ;
            string value = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
            string pattern;
            switch (t.Name)
            {
                case "Single":
                case "Double":
                case "Decimal":
                    pattern = @"^[+|-]?\d+(\.\d+)?$";
                    if (!Regex.IsMatch(value, pattern))
                    {
                        MessageBox.Show("请输入数字！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        dataGridView1.Rows[e.RowIndex].Cells[1].Value = null;
                        return;
                    }

                    break;

                case "Int16":
                case "Int32":
                    pattern = @"^[+|-]?\d+$";
                    if (!Regex.IsMatch(value, pattern))
                    {
                        MessageBox.Show("请输入数字！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        dataGridView1.Rows[e.RowIndex].Cells[1].Value = null;
                        return;
                    }
                    break;

                case "String":
                    return;
            }

        }
    }
}
